﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

using Gift.Model;
using System.Text;
using System.Data;
using System.IO;
using Gift.WebApp;

namespace WebApp.admin.basic
{
    public partial class freightWay : BaseAdmin
    {

        tbFreightWay oFreightWay;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                this.BindFreightTemplateList();
                this.BindDeliveryWay();
                this.BuildForm();
                this.RegionBind();
            }
        }

        protected string tid
        {
            get
            {
                return Request.QueryString["tid"];
            }
        }
        protected string gid
        {
            get
            {
                return Request.QueryString["gid"];
            }
        }



        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(base.Request.QueryString["id"]))
            {
                oFreightWay = BaseFactory.theFreightWay.GetModel(int.Parse(base.Request.QueryString["id"]));
                oFreightWay.deliveryWay = int.Parse(this.ddlDeliveryWay.SelectedItem.Value);
                oFreightWay.defaultFreight = decimal.Parse(this.txtDefaultFreight.Value);
                oFreightWay.freightTemplate = int.Parse(this.ddlFreightTemplate.SelectedItem.Value);
                oFreightWay.increasedFreight = this.txtIncreasedFreight.Value.Trim() == "" ? 0 : decimal.Parse(this.txtIncreasedFreight.Value);
                oFreightWay.region = base.Request.Form["hidRegion"];
                if (BaseFactory.theFreightWay.Update(oFreightWay))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message",
                        string.Format( "<script language=\"javascript\" defer>alert(\"更新成功！\");window.location.href='freightWayList.aspx?tid={0}&gid={1}';</script>"
                                      , tid
                                      ,gid)
                      );
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
                

            }else
            {
                oFreightWay = new tbFreightWay();
                oFreightWay.deliveryWay = int.Parse(this.ddlDeliveryWay.SelectedItem.Value);
                oFreightWay.defaultFreight = decimal.Parse(this.txtDefaultFreight.Value);
                oFreightWay.freightTemplate = int.Parse(this.ddlFreightTemplate.SelectedItem.Value);
                oFreightWay.increasedFreight = this.txtIncreasedFreight.Value.Trim() == "" ? 0 : decimal.Parse(this.txtIncreasedFreight.Value);
                oFreightWay.createTime = DateTime.Now;
                oFreightWay.region = base.Request.Form["hidRegion"];
                oFreightWay.CompanyId = int.Parse(base.Administrator.CompanyId);
                if (BaseFactory.theFreightWay.Add(oFreightWay) > 0)
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message",
                        string.Format("<script language=\"javascript\" defer>alert(\"添加成功！\");window.location.href='freightWayList.aspx?tid={0}&gid={1}';</script>"
                                      , tid
                                      , gid)
                      );
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                }
            }

            
        }


        protected StringBuilder RegionBind()
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                this.RecursionRegion(sb, 0, int.Parse(base.Administrator.CompanyId));
            }
            catch
            {
            }
            return sb;
        }

        private void RecursionRegion(StringBuilder sb, int parentRegion, int companyId)
        {
            DataTable table = BaseFactory.theRegion.GetRegionByParentRegionAndCompanyId(parentRegion, companyId);
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    string str = "";
                    int num = Convert.ToInt32(row["depth"]);
                    if (num > 0)
                    {
                        for (int i = 0; i < num; i++)
                        {
                            str = str + "&nbsp;&nbsp;&nbsp;&nbsp;";
                        }
                        str = str + "┣";
                    }
                    sb.AppendLine("<option value=\"" + row["id"].ToString() + "\">" + str + row["regionName"].ToString() + "</option>");
                    this.RecursionRegion(sb, int.Parse(row["id"].ToString()), companyId);
                }
            }
        }

        // Methods
        protected StringBuilder RegionRightBind()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                if(!string.IsNullOrEmpty(base.Request.QueryString["id"]))
                {
                    oFreightWay = BaseFactory.theFreightWay.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                    ArrayList list = ArrayList.Adapter(oFreightWay.region.Split(new char[] { ',' }));
                    foreach (string str in list)
                    {
                        tbRegion oRegion = BaseFactory.theRegion.GetModel(Convert.ToInt32(str));
                        if (oRegion != null)
                        {
                            builder.AppendLine(string.Concat(new object[] { "<option value=\"", oRegion.id, "\">", oRegion.regionName, "</option>" }));
                        }
                    }
                }
 
            }
            catch
            {
            }
            return builder;
        }

        void BindFreightTemplateList()
        {
            DataTable dtTemplate = BaseFactory.theFreightTemplate.GetFreightTemplateByCompanyId(int.Parse(base.Administrator.CompanyId));
            this.ddlFreightTemplate.DataTextField = "freightTemplateName";
            this.ddlFreightTemplate.DataValueField = "id";
            this.ddlFreightTemplate.DataSource = dtTemplate;
            this.ddlFreightTemplate.DataBind();
            this.ddlFreightTemplate.Items.Insert(0, new ListItem("选择运费模版...", ""));
        }

        void BindDeliveryWay()
        {
            DataTable dtDeliveryWay = BaseFactory.theDeliveryWay.GetDeliveryWayByCompanyId(int.Parse(base.Administrator.CompanyId));
            this.ddlDeliveryWay.DataTextField = "deliveryWayName";
            this.ddlDeliveryWay.DataValueField = "id";
            this.ddlDeliveryWay.DataSource = dtDeliveryWay;
            this.ddlDeliveryWay.DataBind();
            this.ddlDeliveryWay.Items.Insert(0, new ListItem("选择配送方式...", ""));
        }

        void BuildForm()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["id"]))
            {
                oFreightWay = BaseFactory.theFreightWay.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                this.ddlFreightTemplate.Items.FindByValue(oFreightWay.freightTemplate.ToString()).Selected = true;
                this.ddlDeliveryWay.Items.FindByValue(oFreightWay.deliveryWay.ToString()).Selected = true;
                this.txtDefaultFreight.Value = oFreightWay.defaultFreight.ToString();
                this.txtIncreasedFreight.Value = oFreightWay.increasedFreight.ToString();
            }
 
        }


    }
}